<?php
class GalleriesController extends AppController{
	public $helpers = array('Html', 'Form');
	public $components = array('Session');
	
	public function beforeFilter() {
		$this->Auth->deny('index');
	}
	
	public $paginate = array(
        'limit' => 2,
		'order' => array(
            'Gallery.id' => 'desc'
        )
    );
	
	function index(){
		$this->loadModel('Upload');
		$this->set('title_for_layout', 'Gallery');
		
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$this->set('base_url',$base_url);
		$dir = "upload/banner/";
		
		
		$this->set('galleries',$this->paginate('Gallery'));
		
		if ($this->request->is('post')) {
		
			$this->request->data['Gallery']['user_id'] = $this->Auth->user('id'); //Added this line 
			
			$datas = $this->request->data;
			// print "<pre>";
			// print_r($datas);
			// print "</pre>";
			// exit();
			$banner_image = $datas['Gallery']['image'];
			$upload = $this->Upload->upload_image($banner_image,$dir);
			
			if($upload['status']){
				$datas['Gallery']['image'] = $upload['name'];
			}else{
				$this->Session->setFlash('Invalid Image.');
				$this->redirect(array('action' => 'index'));
			}
			
			if ($this->Gallery->save($datas)) {
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
	
	function delete($id){
		
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		
		if ($this->Gallery->delete($id)) {
			$this->Session->setFlash('The menu with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
		
	}
	
}
?>